package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.domain.ProdProp;
import org.example.domain.ProdPropValue;
import org.example.model.Result;
import org.example.service.ProdPropService;
import org.example.service.ProdPropValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="商品规格接口管理")
@RequestMapping("prod/spec")
@RestController
public class ProdSpecController {

    @Autowired
    private ProdPropService prodPropService;

    @Autowired
    private ProdPropValueService prodPropValueService;

    @ApiOperation("多条件分页查询商品规格")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<Page<ProdProp>> loadProdSpecPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam(required = false) String propName) {
        // 多条件分页查询商品规格
        Page<ProdProp> page = prodPropService.queryProdSpecPage(current, size, propName);
        return Result.success( page);
    }

    @ApiOperation("新增商品规格")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:spec:save')")
    public Result<String> saveProdSpec(@RequestBody ProdProp prodProp) {
        Boolean saved = prodPropService.saveProdSpec(prodProp);
        return Result.handle( saved);
    }

    @ApiOperation("修改商品规格信息")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:spec:update')")
    public Result<String> modifyProdSpec(@RequestBody ProdProp prodProp) {
        Boolean modified = prodPropService.modifyProdSpec(prodProp);
        return Result.handle(modified);
    }

    @ApiOperation("删除商品规格")
    @DeleteMapping("/{prodId}")
    @PreAuthorize("hasAuthority('prod:spec:delete')")
    public Result<String> deleteProdSpec(@PathVariable Long prodId) {
        Boolean deleted = prodPropService.removeProdSpecByPropId(prodId);
        return Result.handle(deleted);
    }

    @ApiOperation("查询系统商品属性集合")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<List<ProdProp>> loadProdPropList() {
        List<ProdProp> prodProps = prodPropService.queryProdPropList();
        return Result.success( prodProps);
    }

    @ApiOperation("根据商品属性id查询属性值集合")
    @GetMapping("/listSpecValue/{propId}")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<List<ProdPropValue>> loadProdPropValues(@PathVariable Long propId) {
        List<ProdPropValue> prodPropValues = prodPropValueService.list( new LambdaQueryWrapper<ProdPropValue>()
                .eq( ProdPropValue::getPropId,  propId));
        return Result.success( prodPropValues);
    }

}
